package daoImpl;

import java.util.List;

import javax.persistence.EntityManager;

import modelo.Deporte;
import modelo.Horario;
import modelo.Reserva;
import dao.DAOGeneral;
import dao.IHorarioDAO;

public class HorarioDAOImpl extends DAOGeneral implements IHorarioDAO {

	
	private EntityManager em;

	public HorarioDAOImpl() {
		em = getEntityManager();
	}
	
	public void insertar(Horario hor) {
		em.getTransaction().begin();
		em.persist(hor);
		em.getTransaction().commit();

	}

	public Horario update(Horario hor) {
		em.getTransaction().begin();
		Horario horx = em.find(Horario.class, hor.getIdHorario());
		horx.setDesHorario(hor.getDesHorario());
		if(hor.getReservas()!=null)
		{
			horx.setReservas(hor.getReservas());
		}
		 em.getTransaction().commit();
		 return em.find(Horario.class, hor.getIdHorario());
	}

	public void borrar(Horario hor) {
		em.remove(hor);

	}

	public Horario seleccionarPorId(int id) {
		return em.find(Horario.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Reserva> seleccionarReservasPorHorario(int id_horario)
	{
		return (List<Reserva>)em.createNamedQuery("Reserva.seleccionarReservasPorHorario").setParameter("id_horario",id_horario).getResultList();
	}
}
